package com.ztgf.message.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.message.model.bo.MessageNoticeBO;
import com.ztgf.message.model.bo.MessageNoticeSaveBO;
import com.ztgf.message.model.dto.MessageNoticeDTO;
import com.ztgf.message.model.service.IMessageNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

/**
 * 通知消息控制器
 *
 * @author YUE
 * @Date 2020-05-28 16:21:07
 */

@RestController
@RequestMapping("/cms/messageNotice")
@Slf4j
@Api(value = "MessageNoticeController", tags = "公告消息接口")
public class MessageNoticeController {

    @DubboReference
    private IMessageNoticeService messageNoticeService;

    @PostMapping("/updatePhById")
    @ApiOperation(value = "消息恢复根据Id", notes = "消息恢复根据Id")
    public Result<?> updatePhById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        messageNoticeService.recoverPhById(id);
        return new Result<>();
    }

    @PostMapping("/deletePhById")
    @ApiOperation(value = "物理删除根据Id", notes = "物理删除根据Id")
    public Result<?> deletePhById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        messageNoticeService.updateMessageDeleteByIds(Collections.singletonList(id.toString()));
        return new Result<>();
    }

    @ApiOperation(value = "添加公告消息", notes = "添加公告消息")
    @PostMapping(value = "/insert")
    public Result<?> insert(MessageNoticeSaveBO saveBO, HttpServletRequest request) {
        boolean flag = messageNoticeService.add(saveBO, request.getHeader("sessionId"));
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @ApiOperation(value = "修改公告消息", notes = "修改公告消息")
    @PostMapping("/update")
    public Result update(MessageNoticeSaveBO saveBO) {
        MessageNoticeDTO dto = new MessageNoticeDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        dto.setUpdateTime(new Date());
        boolean flag = messageNoticeService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
            , @ApiImplicitParam(name = "status", value = "状态 0正常，1删除", required = true, paramType = "query", dataType = "Integer")
    })
    public Result updateStatus(Long id, Integer status) {
        MessageNoticeDTO dto = new MessageNoticeDTO();
        dto.setId(id);
        dto.setStatus(status);
        boolean flag = messageNoticeService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除公告消息", notes = "删除公告消息")
    @ApiImplicitParam(name = "id", value = "通知消息id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = messageNoticeService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除公告消息", notes = "批量删除公告消息")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        messageNoticeService.updateMessageDeleteByIds(Arrays.asList(arr));
        //messageNoticeService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<MessageNoticeDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        MessageNoticeDTO dto = messageNoticeService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "公告列表", notes = "公告列表")
    @PostMapping("/queryPageList")
    public Result<Page<MessageNoticeDTO>> queryPageList(MessageNoticeBO bo) {
        Wrapper<MessageNoticeDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("deleted", 0);
//        wrapper.orderBy("is_top", true);
        wrapper.orderBy("sort", true);//按顺序排列
//        wrapper.orderBy("update_time", false);//按更新时间排序
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (StrUtils.isNotEmpty(bo.getStartDate())) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (StrUtils.isNotEmpty(bo.getEndDate())) {
            wrapper.le("create_time", bo.getEndDate());
        }
        if (StrUtils.isNotEmpty(bo.getTitle())) {
            wrapper.like("title", bo.getTitle());
        }
        if (null != bo.getPublisherId()) {
            wrapper.eq("publisher_id", bo.getPublisherId());
        }

        Page<MessageNoticeDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = messageNoticeService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "置顶公告", notes = "置顶公告")
    @PostMapping("/getTopNotice")
    public Result<MessageNoticeDTO> getTopNotice() {
        Wrapper<MessageNoticeDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_top", 0);
        wrapper.eq("deleted", 0);
        MessageNoticeDTO messageNoticeDTO = messageNoticeService.selectOne(wrapper);
        return new Result<>(messageNoticeDTO);
    }

    @PostMapping("/updateSort")
    @ApiOperation(value = "修改排序号", notes = "修改排序号")
    public Result<?> updateSort(Long id, Integer sort) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return messageNoticeService.updateSort(id, sort);
    }

    @PostMapping("/updateIsTop")
    @ApiOperation(value = "修改是否置顶", notes = "修改是否置顶")
    public Result<?> updateIsTop(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return messageNoticeService.updateIsTop(id);
    }

    @PostMapping("/updateIsHot")
    @ApiOperation(value = "修改是否热门", notes = "修改是否热门")
    public Result<?> updateIsHot(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return messageNoticeService.updateIsHot(id);
    }

    @PostMapping("/updateHideFromView")
    @ApiOperation(value = "修改显示，隐藏", notes = "修改显示，隐藏")
    public Result<?> updateHideFromView(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = messageNoticeService.updateHideFromView(id);
        if (flag) {
            return new Result<>(CodeEnum.SUCCESS);
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }
}
